package com.scuec.trainingmanagementsys.service.impl;

import com.scuec.trainingmanagementsys.entity.Discussion;
import com.scuec.trainingmanagementsys.mapper.DiscussionMapper;
import com.scuec.trainingmanagementsys.service.DiscussionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DiscussionServiceImpl implements DiscussionService {

    @Autowired
    private DiscussionMapper discussionMapper;

    @Override
    public List<Discussion> getAllDiscussions() {
        return discussionMapper.getAllDiscussions();
    }

    @Override
    public Discussion getDiscussionById(Integer discussionId) {
        return discussionMapper.getDiscussionById(discussionId);
    }

    @Override
    public int deleteDiscussion(Integer discussionId) {
        return discussionMapper.deleteDiscussion(discussionId);
    }

    @Override
    public List<Discussion> findDiscussionsByStudentNumber(String studentNumber) {
        return discussionMapper.findByStudentNumber(studentNumber);
    }

    @Override
    public List<Discussion> findByStudentId(String studentId) {
        return discussionMapper.findByStudentId(studentId);
    }

    @Override
    public int insertDiscussion(Discussion discussion) {
        // 确保学生ID不为空
        if (discussion.getStudentId() == null) {
            throw new IllegalArgumentException("学生ID不能为空");
        }
        // 设置当前时间
        discussion.setCreateTime(new Date());
        // 确保教师ID为null
        discussion.setTeacherId(null);

        return discussionMapper.insertDiscussion(discussion);
    }

    @Override
    public List<Discussion> findDiscussionsByTeacherNumber(String teacherNumber) {
        return discussionMapper.findDiscussionsByTeacherNumber(teacherNumber);
    }

    @Override
    public int insertDiscussionByTeacher(Discussion discussion) {

        // 设置当前时间
        discussion.setCreateTime(new Date());
        // 确保教师ID为null
        discussion.setStudentId(null);
        return discussionMapper.insertDiscussionByTeacher(discussion);
    }

    @Override
    public int deleteTeacherDiscussion(Integer discussionId) {
        return discussionMapper.deleteDiscussion(discussionId);
    }
}